package ruru.server.backend;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ruru.client.ui.widgets.sortableTable.ComparableLabel;

/**
 * A participant in a 20 man raid. Max bids are tracked here over time, and the
 * official bid increments and decrements are recorded here.
 * 
 * @author zane.tian@gmail.com
 */
public class Raider implements Comparable<Raider> {
	private String raidClass;
	private String name;
	private Date lastRaid;

	private double points;
	private int itemsReceived;
	private int raidCounter;
	private boolean retired;

	private List<DkpChange> dkpChanges;

	public Raider(String name, String raidClass) {
		this.name = name;
		this.raidClass = raidClass;

		lastRaid = null;
		points = 0;
		itemsReceived = 0;
		raidCounter = 0;
		retired = false;
		dkpChanges = new ArrayList<DkpChange>();
	}

	public List<DkpChange> getDkpChanges() {
		return dkpChanges;
	}

	public void addDkpChange(String desc, double value, Date date) {
		dkpChanges.add(new DkpChange(desc, value, date));
	}

	public void setRetired() {
		retired = true;
	}

	public boolean isRetired() {
		return retired;
	}

	/**
	 * Adds n to the raider's point total.  Note that n may be negative.
	 * @param n
	 */
	public void modifyPoints(double n) {
		this.points += n;
		/*		if(maxBid < 18) {
		 maxBid = 18;
		 }*/
	}

	/**
	 * Increments items received for this raider.
	 */
	public void gotItem() {
		itemsReceived++;
	}

	/**
	 * Used for items received through a min or half bid.  Fun items don't count.
	 * TODO: make a separate counter for total items received?
	 */
	public int getItemsReceived() {
		return itemsReceived;
	}

	public void incrementRaidCounter() {
		raidCounter++;
	}

	public int getRaidCounter() {
		return raidCounter;
	}

	public double getLiquidity() {
		if(raidCounter >= 10) {
			return itemsReceived / (double) raidCounter;
		} else
			return 0.0;
	}

	public void setLastRaidDate(Date date) {
		this.lastRaid = new Date(date.getTime());
	}

	public Date getLastRaidDate() {
		if(lastRaid == null) {
			return new Date(0l);
			// throw new IllegalStateException("Raider " + name + " hasn't participated in any raids");
		}

		return lastRaid;
	}

	/**
	 * Returns the maximum bid of this raider.
	 * 
	 * @return maxbid
	 */
	public double getPoints() {
		return points;
	}

	public String getRaidClass() {
		return raidClass;
	}

	public ComparableLabel getColorLabel() {
		ComparableLabel result = new ComparableLabel(toTitleCase(raidClass));
		String style = raidClass;

		result.addStyleName(style);
		result.addStyleName("wowClassLabel");

		return result;
	}

	public String getColoredHtmlClassString() {
		String style = raidClass;
		return "<span class='" + style + "'>" + toTitleCase(raidClass) + "</span>";
	}
	
	public String getColoredHtmlNameString() {
		String style = raidClass;
		return "<span class='" + style + "'>" + toTitleCase(name) + "</span>";
	}

	/**
	 * Capitalizes first letter :P
	 */
	public String toString() {
		return toTitleCase(name);
	}

	public String getName() {
		return name;
	}

	private String toTitleCase(String target) {
		String first = String.valueOf(target.charAt(0));
		String rest = target.substring(1, target.length());

		return first.toUpperCase() + rest;
	}

	public int compareTo(Raider arg0) {
		Raider participant = arg0;

		if(raidClass.compareTo(participant.raidClass) == 0) {
			return name.compareTo(participant.name);
		} else
			return raidClass.compareTo(participant.raidClass);
	}
}